package com.activity.shop;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

import com.activity.adapter.MonthAdapter;
import com.activity.bean.EmployerBean;
import com.activity.bean.UserBean;
import com.api.param.RecordPageParam;
import com.api.viewModel.StoreVM;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.activity.setting.BaseListViewActivity;
import com.example.apple.ddhr_new.activity.widget.Dialog;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.example.apple.ddhr_new.utils.ToastUtil;
import com.example.lzbase.BaseViewModelActivity;
import com.google.gson.Gson;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MemberSheetActivity extends BaseListViewActivity<StoreVM> {

    RecordPageParam pageParam =new RecordPageParam();
    MonthAdapter monthAdapter ;
    private UserBean employerBean;

    private int type;
    @Override
    protected int titleText() {
        return R.string.app_name;
    }

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {
        super.created(savedInstanceState);

          Gson gson = new Gson();


         employerBean =gson.fromJson(getIntent().getExtras().getString("param"), UserBean.class);

         pageParam=gson.fromJson(getIntent().getExtras().getString("page"), RecordPageParam.class);
         pageParam.userid = employerBean.getUserid();
         pageParam.pagenum="1";
        type =getIntent().getExtras().getInt("type");
        
         setTitleText(employerBean.getUsernickname());
        refreshLayout.setEnableLoadMore(true);
        monthAdapter = new MonthAdapter(this);
        monthAdapter.hasHeader=false;
        recycleView.setAdapter(monthAdapter);

        monthAdapter.setOnChildClickListener(new GroupedRecyclerViewAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(GroupedRecyclerViewAdapter adapter, BaseViewHolder holder, int groupPosition, int childPosition) {


                Bundle bundle = new Bundle();

                Gson gson = new Gson();
                bundle.putString("param",gson.toJson(monthAdapter.getData().get(childPosition)));
                bundle.putInt("type",type);
                ActivityUtil.startActivity(MemberSheetActivity.this,ClockDetailActivity.class,bundle);
            }
        });
        monthAdapter.monthListener = new MonthAdapter.MonthListener() {
            @Override
            public void beginTimeClick() {
                Calendar min = Calendar.getInstance();
                Calendar max = Calendar.getInstance();
                Calendar selectDate = Calendar.getInstance();
                min.set(1900,1,1);
                max.setTime(new Date());

                TimePickerView builder = Dialog.popTime(MemberSheetActivity.this,min,max, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        String dateString = simpleDateFormat.format(date);
                        monthAdapter.beginTime = dateString;
                        pageParam.startworktime=dateString;
                        monthAdapter.notifyDataChanged();


                    }
                });


                // 如果不设置的话，默认是系统时间*/


                if(monthAdapter.beginTime==null){
                    selectDate.setTime(new Date());
                }else
                {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        selectDate.setTime(simpleDateFormat.parse(monthAdapter.beginTime));
                    } catch (ParseException e) {

                    }
                }
                builder.setDate(selectDate);
                builder.show();
            }

            @Override
            public void endTimeClick() {
                Calendar min = Calendar.getInstance();
                Calendar max = Calendar.getInstance();
                Calendar selectDate = Calendar.getInstance();
                min.set(1900,1,1);
                max.setTime(new Date());

                TimePickerView builder = Dialog.popTime(MemberSheetActivity.this,min,max, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        String dateString = simpleDateFormat.format(date);
                        monthAdapter.endTime = dateString;
                        pageParam.endworktime=dateString;

                        monthAdapter.notifyDataChanged();


                    }
                });

                // 如果不设置的话，默认是系统时间*/


                if(monthAdapter.endTime==null){
                    selectDate.setTime(new Date());
                }else
                {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        selectDate.setTime(simpleDateFormat.parse(monthAdapter.endTime));
                    } catch (ParseException e) {

                    }
                }
                builder.setDate(selectDate);
                builder.show();
            }

            @Override
            public void queryClick() {

                if(pageParam.startworktime==null){

                    ToastUtil.toast(MemberSheetActivity.this,"请选择开始时间");
                    return;
                }
                if(pageParam.endworktime==null){

                    ToastUtil.toast(MemberSheetActivity.this,"请选择结束时间");
                    return;
                }

                headerFreshing();
            }
        };


        listViewModel.recordPage.observe(this,data->{

            if(data.isHeader){

                monthAdapter.getData().clear();
                monthAdapter.getData().addAll(data.getResult());

                headerFreshEnd();
            }else
            {
                monthAdapter.getData().addAll(data.getResult());
                if(data.getResult().size()>0){
                    footerFreshEnd();
                }else
                {
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
            monthAdapter.notifyDataSetChanged();

            if(monthAdapter.getData().size()>0){
                showEmpty(false);
            }else {
                showEmpty(true);
            }


        });
        listViewModel.recordPageError.observe(this,res->{
            if(res){
                headerFreshEnd();
            }else {
                pageParam.pagenum = (Integer.valueOf(pageParam.pagenum)-1)+"";
                footerFreshEnd();
            }
        });



        headerFreshing();

    }
    @Override
    protected void headerFreshing() {
        pageParam.pagenum ="1";
        refreshLayout.resetNoMoreData();
        addSubscription(listViewModel.getMemberPageRecord(pageParam,true,type));
    }

    @Override
    protected void footerFreshing() {
        pageParam.pagenum = (Integer.valueOf(pageParam.pagenum)+1)+"";

        addSubscription(listViewModel.getMemberPageRecord(pageParam,false,type));
    }
}
